package leetcode.n2021;

/**
 * @program: brushing
 * @description: https://leetcode-cn.com/problems/encode-and-decode-tinyurl/
 * TinyURL 的加密与解密
 * @author: LiuQiang
 * @create: 2019-04-04 14:37
 **/
public class TinyURL {

    // Encodes a URL to a shortened URL.
    public String encode(String longUrl) {
        String rs = "";
        String url[] = longUrl.split("");
        for (int i = 0; i < url.length; i++) {
            char c[] = url[i].toCharArray();
            for (int j = 0; j < c.length; j++) {
                int c1 = c[j];
                c1 += 1;
                c[j] = (char)c1;
            }
            rs += String.valueOf(c);
        }
        return rs;
    }

    // Decodes a shortened URL to its original URL.
    public String decode(String shortUrl) {
        String rs = "";
        String url[] = shortUrl.split(".com");
        for (int i = 0; i < url.length; i++) {
            char c[] = url[i].toCharArray();
            for (int j = 0; j < c.length; j++) {
                int c1 = c[j];
                c1 -= 1;
                c[j] = (char)c1;
            }
            rs += String.valueOf(c);
        }
        return rs;
    }

    public static void main(String[] args) {
        TinyURL tinyURL = new TinyURL();

        String u = tinyURL.encode("https://bone.example.net/?brother=agreement&beds=bird");
        String y = tinyURL.decode(u);
        System.out.println(u);
        System.out.println(y);
    }
}
